<template>
    <div style="float:left" :class="wrapperClass">
        <TextType @calexp="handle" @checkError="checkError" v-if="inputType=='text'"  :values="values" :valueIndex="valueIndex" :inputConfig="inputConfig"/>
        <NumberType @calexp="handle" @checkError="checkError" v-if="inputType=='number'"  :values="values" :value-names="valueNames" :valueIndex="valueIndex" :inputConfig="inputConfig"/>
        <DateType @calexp="handle" @checkError="checkError" v-if="inputType=='date'"  :values="values" :valueIndex="valueIndex" :inputConfig="inputConfig"/>
        <DataDicType @calexp="handle" @checkError="checkError" :is-in-opt="isInOpt" v-if="inputType=='dic'"  :values="values" :value-names="valueNames" :valueIndex="valueIndex" :inputConfig="inputConfig"/>
        <CustDicType @calexp="handle" @checkError="checkError" :is-in-opt="isInOpt" :atomKey="atomKey" v-if="inputType=='custDic'"  :values="values" :value-names="valueNames" :valueIndex="valueIndex" :inputConfig="inputConfig"/>
        <CascaderType @calexp="handle" @checkError="checkError" :atomKey="atomKey" v-if="inputType=='cascader'"  :values="values" :value-names="valueNames" :valueIndex="valueIndex" :inputConfig="inputConfig"/>
        <Tooltip v-if="validateError" :content="errorMessage" theme="light">
            &nbsp;&nbsp;<Icon type="ios-alert-outline" color="#ed4014" />
        </Tooltip>
        <component v-if="inputType==='component'" :placeholder="inputConfig.placeholder" :is-in-opt="isInOpt" :values="values" :value-names="valueNames" :params="inputConfig.params" @valueChange="valueChange" :is="component"/>
    </div>
</template>
<style lang="less" scoped>
</style>
<script>
    import ruiMixin from "_c/rui-auto/rui-rules/src/mixin/rule-mixin";
    import TextType from './input/Text'
    import NumberType from './input/Number'
    import DateType from './input/Date'
    import DataDicType from './input/DataDic'
    import CustDicType from './input/CustDic'
    import CascaderType from './input/Cascader'
    import {fullPath, projectView} from "@/libs/lazyLoading";
    export default {
        name: 'common-type',
        mixins: [ruiMixin],
        components:{
            'TextType':TextType,
            'NumberType':NumberType,
            'DateType':DateType,
            'DataDicType':DataDicType,
            'CustDicType':CustDicType,
            'CascaderType':CascaderType,
        },
        props: {
            atomKey:{
                type:String,
                require: true
            },
            values: {
                type: Array,
                require:true
            },
            valueNames: {
                type: Array,
                require:true
            },
            valueIndex:{
                type:Number,
                require: true
            },
            inputType:{
                type:String,
                require: true
            },
            inputConfig:{
                type:Object,
                require:true
            },
            isInOpt:{
                type:Boolean,
                default:false
            },
            emitValidate:{
                type:Boolean,
                default: false
            }
        },
        data() {
            return {
                wrapperClass:'',
                validateError:false,
                errorMessage:''
            }
        },
        computed: {
            component(){
                if(this.inputConfig&&this.inputConfig.componentPath){
                    if(this.inputConfig.isFull=='1'){
                        return fullPath(this.inputConfig.componentPath);
                    }else{
                        return projectView(this.inputConfig.componentPath);
                    }
                }else{
                    return null;
                }
            }
        },
        methods:{
            handle() {
                this.$emit('calexp');
            },
            checkError(validateError,messages){
                this.validateError = validateError;
                this.errorMessage = messages;
                if(validateError){
                    if(this.emitValidate){
                        this.$emit('validateExpress',false);
                    }
                    this.wrapperClass = 'ivu-form-item-error'
                }else{
                    if(this.emitValidate) {
                        this.$emit('validateExpress', true);
                    }
                    this.wrapperClass ='';
                }
            },
            valueChange(values,valueNames,validate,messages=''){
                this.errorMessage = messages;
                if(!validate){
                    if(this.emitValidate){
                        this.$emit('validateExpress',false);
                    }
                    this.wrapperClass = 'ivu-form-item-error'
                }else{
                    if(this.emitValidate) {
                        this.$emit('validateExpress', true);
                    }
                    this.wrapperClass ='';
                }
                if(this.isInOpt){
                    if(values){
                        this.values.splice(0,this.values.length)
                        this.values.push(...values)
                    }
                    if(valueNames){
                        this.valueNames.splice(0,this.valueNames.length)
                        this.valueNames.push(...valueNames)
                    }
                }else{
                    if(values){
                        this.values.splice(0,this.values.length)
                        this.values.push(values[0]);
                    }
                    if(valueNames){
                        this.valueNames.splice(0,this.valueNames.length)
                        this.valueNames.push(valueNames[0])
                    }
                }
            }
        }
    }
</script>
